package sk.tuke.android4all.dialer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import sk.tuke.android4all.dialer.HistoryItem.CallType;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.util.Log;

public class HistoryAdapter {
	private static String TAG = "HistoryAdapter";
	private Context mContext = null;
    private HistoryItem[] mContainer = null;
    private List<HistoryItem> mAllContacts = null;
    private final int ITEMS_PER_SCREEN = 5;
    private int index = 0;

    public HistoryAdapter(Context mContext_in){
        this.mContext = mContext_in;
        mContainer = new HistoryItem[ITEMS_PER_SCREEN];
        mAllContacts = new ArrayList<HistoryItem>();
        getAllContacts();
    }

    public HistoryItem[] getCurrentSet(){
    	if(mAllContacts.size() < ITEMS_PER_SCREEN){
    		 for(int i = 0; i<ITEMS_PER_SCREEN; i++){
 	        	try{
 	        		mContainer[i] = mAllContacts.get(i);
 	        	}catch(Exception ex){
 	        		mContainer[i] = null;
 	        	}
 	        	
 	        }
    	}else {
	        for(int i = 0; i<ITEMS_PER_SCREEN; i++){
	        	if(i+index >= mAllContacts.size()){
	        		index = -i;
	        	}
	        	try{
	        		mContainer[i] = mAllContacts.get(index +i);
	        	}catch(Exception ex){
	        		mContainer[i] = null;
	        	}
	        	
	        }
    	}
        return mContainer;
    }
    
 
    public void ScrollDown(){
    	index+=ITEMS_PER_SCREEN;
    }
    public boolean scrollNeeded(){
    	return ITEMS_PER_SCREEN<mAllContacts.size();
    }
    public boolean isEmpty(){
    	return mAllContacts.size()==0;
    }
    
    private void getAllContacts() {
        Log.d(TAG,"GetAllHistoryItems");  
        Cursor aCurs = mContext.getContentResolver().query( CallLog.Calls.CONTENT_URI,null, null,null, null);
        mAllContacts = new ArrayList<HistoryItem>();
        
        while ( aCurs.moveToNext() ) {
        	HistoryItem aItem = new HistoryItem();
        	String callerNumber = aCurs.getString( aCurs.getColumnIndex( CallLog.Calls.NUMBER) );
        	String callType = aCurs.getString( aCurs.getColumnIndex( CallLog.Calls.TYPE) );
        	String date = aCurs.getString( aCurs.getColumnIndex( CallLog.Calls.DATE) );
        	
        	aItem.setName(null);
        	aItem.setNumber(callerNumber);
        	aItem.setCallDate(new Date(Long.valueOf(date) ));
        	switch( Integer.parseInt(callType) ) {
        		case CallLog.Calls.OUTGOING_TYPE:
        			aItem.setEvent(CallType.OUTGOING);
        			break;

        		case CallLog.Calls.INCOMING_TYPE:
        			aItem.setEvent(CallType.RECEIVED);
        			break;

        		case CallLog.Calls.MISSED_TYPE:
        			aItem.setEvent(CallType.PASSED);
        			break;
        	}
        	mAllContacts.add(aItem);
        }
        aCurs.close();
    }
    
    public HistoryItem getContactInfo(String id){
    	HistoryItem aContact = null;
    	for(HistoryItem i: mAllContacts){
    		if(i.getId() == id)
    			aContact = i;
    	}
    	return aContact;  	
    }
}
